package com.znf4.controller.timer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;

import com.alibaba.fastjson.JSONObject;
import com.znf4.controller.websocket.SystemWebSocketHandler;
import com.znf4.notice.entity.Notice;
import com.znf4.notice.enums.NoticeStatusEnum;
import com.znf4.notice.server.NoticeServer;

/**
 * 服务器推送 定时器
 * @author agui
 *
 */

@Component
public class WebSocketConroller {
	
	
	
	@Autowired
	private NoticeServer noticeServer ;
	
	
	@Bean
	public SystemWebSocketHandler systemWebSocketHandler() {
		return new SystemWebSocketHandler();
	}
	
	
	/**
	 * 函数功能说明 ： 获取一条公告推送
	 * 方式：websocket 推送
	 * @return
	 */
//	@Scheduled(fixedDelay = 5000 )
	public void socketNotice(){
	
		System.out.println("定时器 调用  WebSocket  启动开始。。。。。。");
		// 无关代码都省略了
		// int unReadNewsCount = 9 ;
		Notice notice = new Notice() ;
		notice.setStatus(NoticeStatusEnum.ACTIVE.getDesc());
		notice = noticeServer.queryOne(notice);
	    
		JSONObject  json = new JSONObject() ;
		json.put("msg1", notice.getContent()) ;
		String result  = json.toJSONString() ;
		
		System.out.println("数据库获取到的给商人的公告 json  ：" + result);
		// systemWebSocketHandler().sendMessageToUsers("13998641323", new
		// TextMessage(jsonLn));
		//systemWebSocketHandler().sendMessageToUser(username , new TextMessage(result));
		 systemWebSocketHandler().sendMessageToUsers(new TextMessage(result));
	}
	
	

}
